<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['pcd']=array('city'=>'');
	$return['params']=array('keyword'=>'','type'=>2,'order'=>'default');
	exi($return);
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('没有找到该商户，请重新选择！','error','jy_wuliu/merch/main');
	}
	//查询该商户具体信息
	$v=pdo_get('jy_wuliu_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$v['images']=$v['images']?json_decode($v['images'],true):array($v['logo']);
	$v['images'][0]=$v['images'][0]?:$v['logo'];
	$v['xianlus']=json_decode($v['xianlus'],true);
	$v['logo']=$v['logo']?tomedia($v['logo']):tomedia('../core/resource/images/nopic.jpg');
	$v['lat_lng']=$v['lat_lng']?:'{}';
	$v['lat_lng']=json_decode($v['lat_lng'],true);
	$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
	$v['juli']=intval($v['juli']/1000);
	$v['looks']=pdo_getsum('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['mid']),'looks');
	$v['jielis']=pdo_count('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['mid']),'id');
	$v['starall']=pdo_getsum('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'merchid'=>$v['mid']),'star');
	$v['starcount']=pdo_count('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'merchid'=>$v['mid'],'star >'=>0),'id');
	$v['stars']=$v['starall']/$v['starcount'];
	$v['stars']=$v['stars']?:5;
	$v['typetitle']=$this->member_types[$v['type']];
	$return['item']=$v;
	exi($return);
}
if($_RQ['op']=='getlist'){
	$setting=$this->plugin_setting('basic');
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$pindex = max(1, intval($post['page']));
	$psize = 1000;
	$total='';
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>'2','isblack'=>0);
	if(trim($_RQ['keyword'])){
		$params['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=intval($_RQ['type']);
	}else{
		$return['params']['type']=2;
	}
	$list=pdo_getslice('jy_wuliu_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['xianlus']=json_decode($v['xianlus'],true);
		$v['logo']=$v['logo']?tomedia($v['logo']):tomedia('../core/resource/images/nopic.jpg');
		$v['lat_lng']=$v['lat_lng']?:'{}';
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		if($v['juli']){
			$v['juli']=intval($v['juli']/1000);
		}
		$v['looks']=pdo_getsum('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['mid']),'looks');
		$v['jielis']=pdo_count('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['mid']),'id');
		$v['starall']=pdo_getsum('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'merchid'=>$v['mid']),'star');
		$v['starcount']=pdo_count('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'merchid'=>$v['mid'],'star >'=>0),'id');
		if($v['starall'] && $v['starcount']){
			$v['stars']=$v['starall']/$v['starcount'];
		}
		$v['stars']=$v['stars']?:5;
		$v['typetitle']=$this->member_types[$v['type']];
	}
	if(trim($_RQ['order'])){
		$return['params']['order']=trim($_RQ['order']);
		if(trim($_RQ['order'])=='default'){
			paixu($list,'juli');
		}elseif(trim($_RQ['order'])=='star'){
			paixu($list,'stars');
		}elseif(trim($_RQ['order'])=='sales'){
			paixu($list,'sales');
		}elseif(trim($_RQ['order'])=='looks'){
			paixu($list,'looks');
		}
	}else{
		paixu($list,'juli');
	}
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	exi($return);
}	